package com.sense.sensedialprehandle.config;

import com.alibaba.fastjson.JSONObject;
import com.sense.sensedialprehandle.annotion.ParamsNotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //如果除了接口的请求还有其他请求的话可以在所有的接口前面加个前缀区分开
        registry.addInterceptor(new CheckParamsHandlerInterceptor()).addPathPatterns("/**");
    }

    private class CheckParamsHandlerInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
            if (!(handler instanceof HandlerMethod)) {
                log.warn("UnSupport handler");
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", "300");
                jsonObject.put("message", "地址有误！");
                response.setHeader("Content-type", "application/json;charset=UTF-8");
                response.getWriter().write(jsonObject.toJSONString());
                return false;
            }
            //拿到该方法上加了注解的参数名称
            List<String> list = getParamsName((HandlerMethod) handler);
            for (String s : list) {
                //获取到参数名称并判断是否为空
                String parameter = request.getParameter(s);
                if (StringUtils.isEmpty(parameter)) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("code", "201");
                    jsonObject.put("message", "参数为空！");
                    response.setHeader("Content-type", "application/json;charset=UTF-8");
                    response.getWriter().write(jsonObject.toJSONString());
                    return false;
                }
            }
            //如果拿到的对象为空,说明没有此注解,直接放行
            return true;
        }

        /**
         * 拿到在参数上加了该注解的参数名称
         */
        private List getParamsName(HandlerMethod handlerMethod) {
            Parameter[] parameters = handlerMethod.getMethod().getParameters();
            List<String> list = new ArrayList<String>();
            for (Parameter parameter : parameters) {
                if (parameter.isAnnotationPresent(ParamsNotNull.class)) {
                    list.add(parameter.getName());
                }
            }
            return list;
        }
    }
}

